package com.his.admin.controller;

import com.his.admin.annotation.LoginInterceptor;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.dto.LoginDto;
import com.his.admin.service.UserService;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.User;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.constant.RedisConstant;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户模块")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/login")
    @ApiOperation(value = "登录",notes = "登录")
    public RestResult login(HttpServletRequest request, @RequestBody User user) {
        return userService.login(request, user);
    }

    @RequestMapping("/logout")
    @ApiOperation(value = "登出",notes = "'登出'")
    public RestResult logon(@RequestHeader("authorization") String authorization) {
        return userService.logon(authorization);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/test")
    public RestResult test() {
        return new RestResultBuilder().success((Object) "ok");
    }

    @RequiresLogin
    @RequestMapping("/userinfo")
    @ApiOperation(value = "获取用户信息",notes = "获取用户信息")
    public RestResult userInfo(@RequestHeader("authorization") String authorization) {
        return userService.userInfo(authorization);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all")
    @ApiOperation(value = "获取用户列表",notes = "获取用户列表")
    public RestResult all(@RequestHeader("authorization") String authorization, @RequestBody ListDto listDto) {
        return userService.all(authorization, listDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all/id")
    @ApiOperation(value = "获取用户id列表",notes = "获取用户id列表")
    public RestResult allId(@RequestBody User user) {
        return userService.allId(user);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all/schedule/id")
    @ApiOperation(value = "获取被排版的用户id列表",notes = "获取被排版的用户id列表")
    public RestResult allScheduleId(@RequestBody User user) {
        return userService.allScheduleId(user);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add")
    @ApiOperation(value = "增加用户",notes = "增加用户")
    public RestResult add(@RequestHeader("authorization")String authorization, @RequestBody User user) {
        Integer roleId = StringUtil.getId(RedisConstant.ROLE, (String) redisTemplate.opsForValue().get(RedisConstant.getRoleAndAuthorization(authorization)));
        if (roleId > user.getRoleId()) {
            return new RestResultBuilder().fail();
        }
        user.setCreateTime(LocalDateTime.now());
        if (userService.save(user)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/update")
    @ApiOperation(value = "更新用户",notes = "更新用户")
    public RestResult update(@RequestHeader("authorization")String authorization, @RequestBody User user) {
        Integer roleId = StringUtil.getId(RedisConstant.ROLE, (String) redisTemplate.opsForValue().get(RedisConstant.getRoleAndAuthorization(authorization)));
        if (roleId > user.getRoleId()) {
            return new RestResultBuilder().fail();
        }
        user.setUpdateTime(LocalDateTime.now());
        if (userService.updateById(user)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/delete")
    @ApiOperation(value = "删除用户",notes = "删除用户")
    public RestResult delete(@RequestBody List<Integer> ids) {
        if (userService.removeByIds(ids)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }
}
